package pre.bookcloud.java21.qoder.todaysign01;

import javax.swing.*;

/**
 * Main Application Entry Point
 * 应用程序启动入口类
 */
public class App {
    
    /**
     * Main method to start the application
     * 启动应用程序的主方法
     * 
     * @param args Command line arguments
     */
    public static void main(String[] args) {
        // Set system properties for better UI rendering
        System.setProperty("swing.aatext", "true");
        System.setProperty("awt.useSystemAAFontSettings", "on");
        
        // Set Look and Feel before creating any GUI components
        // Using default Look and Feel for now
        
        // Ensure GUI creation happens on the Event Dispatch Thread
        SwingUtilities.invokeLater(() -> {
            try {
                // Create and show the main application window
                createAndShowGUI();
            } catch (Exception e) {
                System.err.println("Error starting application: " + e.getMessage());
                e.printStackTrace();
                
                // Show error dialog to user
                JOptionPane.showMessageDialog(
                    null,
                    "应用程序启动时发生错误：\n" + e.getMessage(),
                    "启动错误",
                    JOptionPane.ERROR_MESSAGE
                );
                
                System.exit(1);
            }
        });
    }
    
    /**
     * Create and show the main GUI
     * 创建并显示主界面
     */
    private static void createAndShowGUI() {
        try {
            // Show splash screen (optional)
            showSplashScreen();
            
            // Create the main frame
            MainFrame mainFrame = new MainFrame();
            
            // Show the main frame
            mainFrame.showFrame();
            
            // Print startup information to console
            printStartupInfo();
            
        } catch (Exception e) {
            throw new RuntimeException("Failed to create main GUI", e);
        }
    }
    
    /**
     * Show a simple splash screen (optional)
     * 显示启动画面（可选）
     */
    private static void showSplashScreen() {
        try {
            // Create a simple splash dialog
            JDialog splash = new JDialog();
            splash.setUndecorated(true);
            splash.setSize(400, 200);
            splash.setLocationRelativeTo(null);
            
            // Create splash content
            JPanel splashPanel = new JPanel();
            splashPanel.setBackground(new java.awt.Color(245, 245, 245));
            splashPanel.setBorder(BorderFactory.createLineBorder(java.awt.Color.GRAY, 2));
            splashPanel.setLayout(new java.awt.BorderLayout());
            
            JLabel titleLabel = new JLabel("<html><center><h1>学生签到管理系统</h1><p>正在启动...</p></center></html>", 
                                         JLabel.CENTER);
            titleLabel.setFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 16));
            
            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true);
            progressBar.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
            
            splashPanel.add(titleLabel, java.awt.BorderLayout.CENTER);
            splashPanel.add(progressBar, java.awt.BorderLayout.SOUTH);
            
            splash.add(splashPanel);
            splash.setVisible(true);
            
            // Simulate loading time
            Thread.sleep(1500);
            
            splash.dispose();
            
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            // Continue without splash screen
        } catch (Exception e) {
            // Continue without splash screen if there's any error
            System.err.println("Could not show splash screen: " + e.getMessage());
        }
    }
    
    /**
     * Print startup information to console
     * 在控制台打印启动信息
     */
    private static void printStartupInfo() {
        System.out.println("====================================");
        System.out.println("学生签到管理系统");
        System.out.println("====================================");
        System.out.println("版本: 1.0.0");
        System.out.println("Java版本: " + System.getProperty("java.version"));
        System.out.println("操作系统: " + System.getProperty("os.name") + " " + System.getProperty("os.version"));
        System.out.println("启动时间: " + java.time.LocalDateTime.now().format(
            java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println("====================================");
        System.out.println("应用程序启动成功！");
        System.out.println("====================================");
    }
    
    /**
     * Get application information
     * 获取应用程序信息
     */
    public static class AppInfo {
        public static final String NAME = "学生签到管理系统";
        public static final String VERSION = "1.0.0";
        public static final String DEVELOPER = "Java21 Development Team";
        public static final String DESCRIPTION = "基于Java 21和Swing开发的学生日常签到管理系统";
        
        private AppInfo() {
            // Utility class, prevent instantiation
        }
    }
}